package cn.yav.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * All rights Reserved, Designed By www.yavyun.net
 *
 * @version V1.0
 * @Title: yavpingtai
 * @Package cn.yav.date
 * @Description: ${todo}
 * @author: zn
 * @date: 2019/11/29 18:23
 * @Copyright: 2019 www.yavyun.net Inc. All rights reserved.
 * 注意：本内容仅限于浙江智飨科技内部传阅，禁止外泄以及用于其他的商业目
 */
public class InventoryTimeHourUtils {
    public static String inventoryTimeHour(String outputtime,String shelfunit,float shelflife) throws java.text.ParseException{
        if(("年").equals(shelfunit)){
            int cc=(int)shelflife*365;
            String expirationTime=getDate(outputtime,cc);
            return expirationTime;
        }else if(("月").equals(shelfunit)){
            int cc=(int)shelflife*30;
            String expirationTime=getDate(outputtime,cc);
            return expirationTime;
        }else if(shelfunit.equals("天")){
            int cc=(int)shelflife;
            String expirationTime=getDate(outputtime,cc);
            return expirationTime;
        }else if(shelfunit.equals("时")){
            int cc=(int)shelflife/24;
            String expirationTime=getDate(outputtime,cc);
            return expirationTime;
        }
        return "";
    }
    public static String getDate(String releaseDate,int day) throws java.text.ParseException{
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        Date d = new Date () ;
        try{
            //字符串转日期,再设置calendar的时间
            calendar.setTime(df.parse(releaseDate));
        }catch(Exception e){
            e.printStackTrace();
        }
        //calendar.add(Calendar.DATE, day);//这句话可以替代下面的两行
        //返回给定日历字段的值
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);
        //将给定的日历字段设置为给定值
        calendar.set(Calendar.DAY_OF_YEAR, day1 + day );
        //返回表示calendar的时间值的 Date 对象
        d= calendar.getTime();
        //日期转字符串
        String  date = df.format(d);
        return date;
    }
}
